import path from 'path'
import { createRouter, createWebHistory } from 'vue-router'
// import Vue from 'vue'
import { Toast } from 'vant';

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        name: 'home',
        component: () => import('../views/HomeView.vue'),
        meta:{
            title:'首页',
            isShow:true
        }
    },
    {
        path: '/search',
        name: 'search',
        component: () => import('../views/searchView.vue'),
        meta:{
            title:'搜索',
            isShow:false
        }
    },
    {
        path: '/details',
        name: 'details',
        component: () => import('../views/detailsView.vue'),
        meta:{
            title:'详情',
            isShow:false
        }
    },
    {
        path: '/cart',
        name: 'cart',
        component: () => import('../views/cartView.vue'),
        meta:{
            title:'购物车',
            isShow:false
        }
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/loginView.vue'),
        meta:{
            title:'登录or注册',
            isShow:true
        }
    },
    {
        path: '/register',
        name: 'register',
        component: () => import('../views/registView.vue'),
        meta:{
            title:'注册',
            isShow:true
        },
        children:[
            {
                path: 'step1',
                name: 'step1',
                component: () => import('../views/registStep1.vue'),
                meta:{
                    title:'注册步骤1',
                    isShow:false
                },
            },
            {
                path: 'step2',
                name: 'step2',
                component: () => import('../views/registStep2.vue'),
                meta:{
                    title:'注册步骤2',
                    isShow:false
                },
            },
            {
                path: 'step3',
                name: 'step3',
                component: () => import('../views/registStep3.vue'),
                meta:{
                    title:'注册步骤3',
                    isShow:false
                },
            }

        ]
    },
    {
        path: '/mine',
        name: 'mine',
        component: () => import('../views/mineView.vue'),
        meta:{
            title:'我的',
            isShow:false
        }
    },
    {
        path: '/address',
        name: 'address',
        component: () => import('../views/addressView.vue'),
        meta:{
            title:'地址管理',
            isShow:false
        }
    },
    {
        path: '/order',
        name: 'order',
        component: () => import('../views/orderView.vue'),
        meta:{
            title:'订单中心',
            isShow:false
        }
    },
    {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: () => import('../views/NotFoundView.vue'),
        meta:{
            title:'404NotFound',
            isShow:false
        }
    }
]
})

//设置一个路由前置守卫
// 判断是否可以进入购物车页面
router.beforeEach((to,from)=>{
    if(to.path=='/cart'){
        if(localStorage.getItem('token')){
            return true
        }else {
            Toast('请先登录');
            return {path:'/login'}    
        }
    }else{
        return true
    }
})
// 全局后置钩子，实现不同页面显示不同标题
 router.afterEach((to, from) => {
    document.title = to.meta.title
})

export default router
